<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * crontab 任务
 *
 * /usr/bin/php /home/web/index.php cron function argvs
 */
class Cron extends CT_Controller
{

    public function __construct()
    {
        parent::__construct();
        
        set_time_limit(0);
//         ini_set('memory_limit','512M');
        
        if(! $this->input->is_cli_request()) 
        {
            echo "greet my only be accessed from the command line";
            exit;
        }
        
//         $this->output->enable_profiler(FALSE);
    }

    /**
     * 用户提前一小时通知上课
     */
    public function book_before()
    {
        $this->load->model('book_model');
        $this->load->model('send_template_msg_log_model');
        require_once(APPPATH . "/third_party/wechat/wxpay/lib/WxTemplate.Api.php");
        $WxTemplateApi = new WxTemplateApi();

        $res = $this->book_model->book_before();

        if(empty($res))
        {
            echo '没有数据，程序退出!' . '\n';
            exit;
        }

        foreach($res as $val)
        {
            $stml_info = $this->send_template_msg_log_model->find(array('book_id'=>$val['id'], 'type'=>Send_template_msg_log_model::TYPE_2));
            if($stml_info){
                continue;
            }
            
            $book_id = id_encode($val['id']);
            $open_id = $val['open_id'];
            
            $template=array(
                'touser' => $open_id,
                'template_id' => "WmRwDwfy9uaSVNXTo7klszBF9j6BsULEbEW_gz-jUeY",    //模板的id
                'url' => DOMAIN_MAIN.'/courserecord',
                'topcolor' => "#173177",
                'data' => array(
                    'first' => array('value'=>urlencode('您好，您预约的课程还有1小时就要开始上课了'),'color'=>"#173177"),
                    'book_id' => array('value'=>urlencode($book_id),'color'=>'#173177'),
                    "remark" => array("value"=>"如有问题，请联系客服！", "color"=>"#173177")
                )
            );
            
            $res = $WxTemplateApi->send_temp_msg(urldecode(json_encode($template)));
            
            $columns = array(
                'book_id'=>$val['id'],
                'res'=>json_encode($res),
                'type'=>Send_template_msg_log_model::TYPE_2,
                'create_date'=>date('Y-m-d H:i:s'),
                'update_date'=>date('Y-m-d H:i:s')
            );
            
            $this->send_template_msg_log_model->insert($columns);
        }

    }
    
    /**
     * 用户下课十分钟后通知点击下课
     */
    public function book_end()
    {
        $this->load->model('book_model');
        $this->load->model('send_template_msg_log_model');
        require_once(APPPATH . "/third_party/wechat/wxpay/lib/WxTemplate.Api.php");
        $WxTemplateApi = new WxTemplateApi();
    
        $res = $this->book_model->book_end();
    
        if(empty($res))
        {
            echo '没有数据，程序退出!' . '\n';
            exit;
        }
    
        foreach($res as $val)
        {
            $stml_info = $this->send_template_msg_log_model->find(array('book_id'=>$val['id'], 'type'=>Send_template_msg_log_model::TYPE_4));
            if($stml_info){
                continue;
            }
    
            $book_id = id_encode($val['id']);
            $open_id = $val['open_id'];
    
            $template=array(
                'touser' => $open_id,
                'template_id' => "WmRwDwfy9uaSVNXTo7klszBF9j6BsULEbEW_gz-jUeY",    //模板的id
                'url' => DOMAIN_MAIN.'/courserecord',
                'topcolor' => "#173177",
                'data' => array(
                    'first' => array('value'=>urlencode('您好，您预约的课程已下课，请确认下课'),'color'=>"#173177"),
                    'book_id' => array('value'=>urlencode($book_id),'color'=>'#173177'),
                    "remark" => array("value"=>"如有问题，请联系客服！", "color"=>"#173177")
                )
            );
    
            $res = $WxTemplateApi->send_temp_msg(urldecode(json_encode($template)));
    
            $columns = array(
                'book_id'=>$val['id'],
                'res'=>json_encode($res),
                'type'=>Send_template_msg_log_model::TYPE_4,
                'create_date'=>date('Y-m-d H:i:s'),
                'update_date'=>date('Y-m-d H:i:s')
            );
    
            $this->send_template_msg_log_model->insert($columns);
        }
    
    }
    
    /**
     * 老师提前一小时通知上课
     */
    public function tbook_before()
    {
        $this->load->model('book_model');
        $this->load->model('send_template_msg_log_model');
        require_once(APPPATH . "/third_party/wechat/wxpay/lib/WxTemplate.Api.php");
        $WxTemplateApi = new WxTemplateApi();
    
        $res = $this->book_model->tbook_before();
    
        if(empty($res))
        {
            echo '没有数据，程序退出!' . '\n';
            exit;
        }
    
        foreach($res as $val)
        {
            $stml_info = $this->send_template_msg_log_model->find(array('book_id'=>$val['id'], 'type'=>Send_template_msg_log_model::TYPE_7));
            if($stml_info){
                continue;
            }
    
            $book_id = id_encode($val['id']);
            $open_id = $val['open_id'];
    
            $template=array(
                'touser' => $open_id,
                'template_id' => "WmRwDwfy9uaSVNXTo7klszBF9j6BsULEbEW_gz-jUeY",    //模板的id
                'url' => DOMAIN_MAIN.'/courserecord',
                'topcolor' => "#173177",
                'data' => array(
                    'first' => array('value'=>urlencode('您好，您预约的课程还有1小时就要开始上课了'),'color'=>"#173177"),
                    'book_id' => array('value'=>urlencode($book_id),'color'=>'#173177'),
                    "remark" => array("value"=>"如有问题，请联系客服！", "color"=>"#173177")
                )
            );
    
            $res = $WxTemplateApi->send_temp_msg(urldecode(json_encode($template)));
    
            $columns = array(
                'book_id'=>$val['id'],
                'res'=>json_encode($res),
                'type'=>Send_template_msg_log_model::TYPE_7,
                'create_date'=>date('Y-m-d H:i:s'),
                'update_date'=>date('Y-m-d H:i:s')
            );
    
            $this->send_template_msg_log_model->insert($columns);
        }
    
    }
    
    /**
     * 老师下课十分钟后通知点击下课
     */
    public function tbook_end()
    {
        $this->load->model('book_model');
        $this->load->model('send_template_msg_log_model');
        require_once(APPPATH . "/third_party/wechat/wxpay/lib/WxTemplate.Api.php");
        $WxTemplateApi = new WxTemplateApi();
    
        $res = $this->book_model->tbook_end();
    
        if(empty($res))
        {
            echo '没有数据，程序退出!' . '\n';
            exit;
        }
    
        foreach($res as $val)
        {
            $stml_info = $this->send_template_msg_log_model->find(array('book_id'=>$val['id'], 'type'=>Send_template_msg_log_model::TYPE_9));
            if($stml_info){
                continue;
            }
    
            $book_id = id_encode($val['id']);
            $open_id = $val['open_id'];
    
            $template=array(
                'touser' => $open_id,
                'template_id' => "WmRwDwfy9uaSVNXTo7klszBF9j6BsULEbEW_gz-jUeY",    //模板的id
                'url' => DOMAIN_MAIN.'/courserecord',
                'topcolor' => "#173177",
                'data' => array(
                    'first' => array('value'=>urlencode('您好，您预约的课程已下课，请确认下课'),'color'=>"#173177"),
                    'book_id' => array('value'=>urlencode($book_id),'color'=>'#173177'),
                    "remark" => array("value"=>"如有问题，请联系客服！", "color"=>"#173177")
                )
            );
    
            $res = $WxTemplateApi->send_temp_msg(urldecode(json_encode($template)));
    
            $columns = array(
                'book_id'=>$val['id'],
                'res'=>json_encode($res),
                'type'=>Send_template_msg_log_model::TYPE_9,
                'create_date'=>date('Y-m-d H:i:s'),
                'update_date'=>date('Y-m-d H:i:s')
            );
    
            $this->send_template_msg_log_model->insert($columns);
        }
    
    }
    
}